<?php

function curl_cook($url){
$nick='CrackDubz.com';
$pass='Iwillnotstealthis';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, 'http://www.megavideo.com/?s=signup');
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch , CURLOPT_POST , 1) ; 
curl_setopt ($ch, CURLOPT_POSTFIELDS, "action=login&nickname={$nick}&password={$pass}"); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cbi-log.txt');
ob_start();     
curl_exec ($ch);
ob_end_clean(); 
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cbi-log.txt");
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}
function get_by_curl($url){
     $ch = curl_init(); 
     curl_setopt ($ch, CURLOPT_URL, $url); 
     curl_setopt ($ch, CURLOPT_USERAGENT, "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
     curl_setopt ($ch, CURLOPT_HEADER, 1); 
	 curl_setopt($ch, CURLOPT_COOKIEFILE, "cbi-log.txt");
     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  //   curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
     curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
     $result = curl_exec ($ch);
     curl_close($ch);

return $result;
}
function mv_decrypt($str_hex, $key1, $key2){
  $str_bin = "";
  for($i = 0; $i < 128; $i++){
    $str_bin .= floor(hexdec($str_hex[floor($i/4)])/pow(2,(3-($i%4))))%2;
  }
  $key = Array();
  for ($i = 0; $i < 384; $i++){
    $key1 = ($key1 * 11 + 77213) % 81371;
    $key2 = ($key2 * 17 + 92717) % 192811;
    $key[$i] = ($key1 + $key2) % 128;
  }
  for ($i = 256; $i >= 0; $i--){
    $temp = $str_bin[$key[$i]];
    $str_bin[$key[$i]] = $str_bin[$i%128];
    $str_bin[$i%128] = $temp;
  }
  for ($i = 0; $i < 128; $i++){
    $str_bin[$i] = $str_bin[$i] ^ $key[$i+256] & 1;
  }
  $str_hex = "";
  for($i = 0; $i < 32; $i++){
    $str_hex .= dechex(bindec(substr($str_bin, $i*4, 4)));
  }
  return $str_hex;
}

function set_megaheader($megavideo_id){
  
  $link_size="http://www.megavideo.com/xml/videolink.php?v=".$megavideo_id;
  $host='megavideo.com';
	$link_content='http://www.megavideo.com/?v='.$megavideo_id;
  if ($content = get_by_curl($link_content)){
    $parameters = Array("flashvars.un", "flashvars.k1", "flashvars.k2", "flashvars.s");
    $success = true;
    for($i=0; $i<Count($parameters); $i++){
      $success = $success && preg_match('/'.$parameters[$i] .' = "([^"]+)"\;/', $content, $match);
	   if($parameters[$i]=='flashvars.un')
	  	$un=$match[1];
	  elseif($parameters[$i]=='flashvars.k1')
	  	$k1=$match[1];
     elseif($parameters[$i]=='flashvars.k2')
	  	$k2=$match[1];
		elseif($parameters[$i]=='flashvars.s')
	  	$s=$match[1];
    }
	$cnt_size=get_by_curl($link_size);
	preg_match('/ size="([^"]+)"/', $cnt_size, $si);
	$size=$si[1];

    if($success){
      $dkey=mv_decrypt($un,$k1,$k2);
      $video_url = "http://www".$s.".".$host."/files/".$dkey."/".$pos;

      header("Content-Type: video/x-flv");
      header("Content-Disposition: attachment; filename=".$megavideo_id.".flv" );
	  header("Content-Transfer-Encoding: binary");
      header("Content-Length: ".$size);
     readfile($video_url);
    }
  }

}

if(isset($_GET["id"])){
$id=trim($_GET["id"]);
$de_id=base64_decode($id);
set_megaheader($de_id);
}
else
header('Location: http://360phim.info');
?>